package Control;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.SystemColor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;

import Entity.Workshop_WorkshopCategory;
import Entity.Workshop_WorkshopEntity;
import Form.Main;
import Form.Workshop_UpdateWorkshop_Form;
import Form.Workshop_ViewWorkshop_Form;
import Form.Workshop_WorkshopMain_Form;

public class Workshop_WorkshopMain_Controller {
	//Object Attributes
	JDialog viewSingle;
	Date startDate;
	String sort, order, workshopName;
	boolean search = false, searchCategory = false, searchWorkshopName = false;
	private Workshop_WorkshopMain_Form parser;
	private Workshop_WorkshopEntity selectedWorkshop;
	private Workshop_WorkshopCategory selectedCategory = new Workshop_WorkshopCategory();
	private ArrayList<Workshop_WorkshopEntity> workshopList = new ArrayList<Workshop_WorkshopEntity>();
	private ArrayList<Workshop_WorkshopCategory> categoryList = new ArrayList<Workshop_WorkshopCategory>();
	
	public JDialog getViewSingle() {
		return viewSingle;
	}

	public void setViewSingle(JDialog viewSingle) {
		this.viewSingle = viewSingle;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public Workshop_WorkshopMain_Form getParser() {
		return parser;
	}

	public void setParser(Workshop_WorkshopMain_Form parser) {
		this.parser = parser;
	}
	
	public Workshop_WorkshopEntity getSelectedWorkshop() {
		return selectedWorkshop;
	}

	public void setSelectedWorkshop(Workshop_WorkshopEntity selectedWorkshop) {
		this.selectedWorkshop = selectedWorkshop;
	}

	public Workshop_WorkshopMain_Controller(final Workshop_WorkshopMain_Form parser){
		this.setParser(parser);
		
		SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>()
		{
			@Override
			protected Void doInBackground() throws Exception {
				categoryList = Workshop_WorkshopCategory.retrieveAllCategory();
				return null;
			}
			
			protected void done(){
				filter();
				
				getParser().getCategory().addItem("Select a category..");
				for(int i = 0; i < categoryList.size(); i++){
					getParser().getCategory().addItem(categoryList.get(i).getDescription());
				}
				parser.getParser().closeProgressBar();
			}
		};
		parser.getParser().getProgressBar();
		worker.execute();
	}
	
	public void filter(){
		filterSort();
		filterOrder();
		generateList();
	}

	public void filterSort(){
		if(getParser().getRdbtnWorkshopName().isSelected()){
			setSort("workshop.name");
		}
		else if(getParser().getRdbtnStartDate().isSelected()){
			setSort("workshop.start_date_time");
		}
		else if(getParser().getRdbtnStatus().isSelected()){
			setSort("workshop.workshop_status_code");
		}
	}
	
	public void filterOrder(){
		if(getParser().getRdbtnAscending().isSelected()){
			setOrder("ASC");
		}
		else if(getParser().getRdbtnDescending().isSelected()){
			setOrder("DESC");
		}
	}
	
	public void search(){
		search = true;
		if(getParser().getCategory().getSelectedIndex() > 0){
			selectedCategory = categoryList.get(getParser().getCategory().getSelectedIndex()-1);
			searchCategory = true;
		}
		else{
			searchCategory = false;
		}
		
		if(!getParser().getTxtWorkshopName().getText().isEmpty()){
			workshopName = getParser().getTxtWorkshopName().getText();
			searchWorkshopName = true;
		}
		else{
			searchWorkshopName = false;
		}
		
		startDate = (Date)(getParser().getstartDate().getValue());
		
		generateList();
	}
	
	
	public static void checkWorkshopStatus(){
		SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>()
		{
			@Override
			protected Void doInBackground() throws Exception {
				if(CommonControls.getUserType().equals("Staff")){
					Workshop_WorkshopEntity.changeWorkshopStatusToCLS();
					Workshop_WorkshopEntity.changeWorkshopStatusToOBS();
				}
				return null;
			}
			
			protected void done(){
				Main.frame.closeProgressBar();
			}
		};
		Main.frame.getProgressBar();
		worker.execute();
	}
	
	public void generateList(){
		SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>()
		{
			@Override
			protected Void doInBackground() throws Exception {
				workshopList = Workshop_WorkshopEntity.retrieveAllWorkshops(getSort(), getOrder(), search, searchCategory, selectedCategory, searchWorkshopName, workshopName, startDate);
				
				return null;
			}
			
			protected void done(){
				getParser().getViewPanel().removeAll();
				getParser().getViewPanel().repaint();
				getParser().getViewPanel().revalidate();
				
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
				getParser().getViewPanel().setPreferredSize(new Dimension(400, workshopList.size() * 35));
				for(int i = 0; i < workshopList.size(); i++){
					final int tempIndex = i;
					final JPanel workshop = new JPanel();
					final JLabel workshopName = new JLabel(workshopList.get(i).getWorkshopName());
					final JLabel status = new JLabel(workshopList.get(i).getStatusCode().getDescription());
					final JLabel startDate = new JLabel(sdf.format(workshopList.get(i).getStartDateTime()));
					
					workshop.setPreferredSize(new Dimension(400, 30));
					workshop.setOpaque(false);
					FlowLayout layout = (FlowLayout)workshop.getLayout();
					layout.setAlignment(FlowLayout.LEFT);
					
					workshop.addMouseListener(new MouseListener() {

						@Override
						public void mouseClicked(MouseEvent arg0) {
							setSelectedWorkshop(workshopList.get(tempIndex));
							setViewSingle(new JDialog(parser.getParser()));
							if(CommonControls.getUserType().equals("Staff") && !selectedWorkshop.getStatusCode().getStatusCode().equals("OBS")){
								viewSingle.add(new Workshop_UpdateWorkshop_Form(parser));
							}
							else{
								viewSingle.add(new Workshop_ViewWorkshop_Form(parser));
							}
							viewSingle.pack();
							viewSingle.setLocationRelativeTo(parser.getParser());
							viewSingle.setVisible(true);
						}

						@Override
						public void mouseEntered(MouseEvent arg0) {
							workshop.setOpaque(true);
							workshop.setBackground(SystemColor.desktop);
							workshopName.setForeground(Color.WHITE);
							status.setForeground(Color.WHITE);
							startDate.setForeground(Color.WHITE);
						}

						@Override
						public void mouseExited(MouseEvent arg0) {
							workshop.setOpaque(false);
							workshop.setBackground(null);
							workshopName.setForeground(null);
							status.setForeground(null);
							startDate.setForeground(null);
						}

						@Override
						public void mousePressed(MouseEvent arg0) {
						}
						@Override
						public void mouseReleased(MouseEvent arg0) {
						}
						
					});

					workshopName.setPreferredSize(new Dimension(167, 20));
					workshopName.setHorizontalAlignment(SwingConstants.LEFT);
					workshop.add(workshopName);

					status.setPreferredSize(new Dimension(75, 20));
					status.setHorizontalAlignment(SwingConstants.CENTER);
					workshop.add(status);

					startDate.setPreferredSize(new Dimension(133, 20));
					startDate.setHorizontalAlignment(SwingConstants.CENTER);
					workshop.add(startDate);
					
					getParser().getViewPanel().add(workshop);
				}
				getParser().getViewPanel().repaint();
				getParser().getViewPanel().revalidate();
				parser.getParser().closeProgressBar();
			}
		};
		parser.getParser().getProgressBar();
		worker.execute();
	}
}
